/*
 * @Author: 刘庆州 
 * @Date: 2022-01-08 17:07:41 
 * @Last Modified by: 刘庆州
 * @Last Modified time: 2022-03-14 10:37:00
 */

import HttpUtils from '../utils/HttpUtil'
// import HttpServices from '../utils/HttpServices'
import UserManager from '../common/UserManager'
import {Toast, InputItem, Modal as AntdModal} from '@ant-design/react-native';
/**
 * 用户登录
 * @param params {loginId, passWord}
 * @returns {Promise<any | never>}
 */
 const userLogin = async (params) => {

    console.log(params);
    return HttpUtils.POST('/Server.svc/api/invoke', params)
      .then(resp => {
        return Promise.resolve(resp);
      })
      .catch(resp => {
        // Toast.info(resp);
        return Promise.reject(resp);
      });

    // return HttpServices.post('/Server.svc/api/invoke', JSON.stringify(params)).then(resp => {
    //   console.log("response:", resp)
    //     return Promise.resolve(resp);
    //   })
    //   .catch(resp => {
    //     console.log("catch response:", resp)
    //     // Toast.showToast(resp);
    //     return Promise.reject(resp);
    //   });
  };

  const generateLongTokenApi = async () =>{
    var userInfo = await UserManager.getUserInfo()
    const ticket = await UserManager.getTicket()
    console.log("ticket is ", ticket)
    if (ticket === null || ticket === undefined){

      return Promise.reject("ticket is null ");
    }
    const contextParam = {
      InvOrgId: userInfo.InvOrg,
      Ticket: ticket === null ? '' : ticket,
    };
    const parameters = [{
      'Value': ticket === null ? '' : ticket,
    }];

    const params = {
      ApiType: 'AppSecurtiyController',
      Context: contextParam,
      Method: 'GenerateLongToken',
      Parameters: parameters,

    };

  

    console.log(params);
    return HttpUtils.POST('/Server.svc/api/invoke', params)
      .then(resp => {
        return Promise.resolve(resp);
      })
      .catch(resp => {
        Toast.showToast(resp);
        return Promise.reject(resp);
      });
  }


  const validateLongTokenApi = async () =>{
    let userInfo = await  UserManager.getUserInfo()
    let ticket = await  UserManager.getLongTicket()
    const contextParam = {
      InvOrgId: userInfo.InvOrg
    };
    const parameters = [{
      'Value': ticket === null ? '' : ticket,
    }];

    const params = {
      ApiType: 'AppSecurtiyController',
      Context: contextParam,
      Method: 'ValidateLongToken',
      Parameters: parameters,

    };

  

    console.log(params);
    return HttpUtils.POST('/Server.svc/api/invoke', params)
      .then(resp => {
        return Promise.resolve(resp);
      })
      .catch(resp => {
        Toast.showToast(resp);
        return Promise.reject(resp);
      });
  }

  module.exports = {
    userLogin, generateLongTokenApi, validateLongTokenApi
  };
  